package com.cyt.web.controller.phone;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.question.model.po.Question;
import com.cyt.work.question.model.vo.QuestionVo;
import com.cyt.work.question.service.IQuestionScoreService;
import com.cyt.work.question.service.IQuestionService;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.service.WxEducationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 问卷调查
 *
 * @author congcong tao
 * @date 2018/12/15 11:26
 */
@RestController
public class PhoneQuestionnaireController extends PhoneController {

    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IQuestionScoreService questionScoreService;
    @Autowired
    private WxEducationUserService wxEducationUserService;

    /**
     * 登录页面
     * @param token
     * @param applyId
     * @author congcong tao
     * @date 2019/9/4 14:55
     */
    @RequestMapping(value = "/phone/question/questionLogin")
    public ModelAndView questionLogin(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/question/questionLogin");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }

    /**
     * 登录
     * @param token
     * @param applyId
     * @param loginName
     * @param password
     * @author congcong tao
     * @date 2019/9/4 16:26
     */
    @BaseLog(module = "问卷调查", classz = "问卷调查", methods = "登录问卷", businessType = BusinessType.INSERT, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/question/login",method = RequestMethod.POST)
    public ReturnResult login(String token, String applyId, String loginName, String password){
        WxMember wxMember = acceptToken(token);
        return wxEducationUserService.login(wxMember, loginName, password);
    }

    /**
     * 问卷调查列表
     * @param token 用户信息
     * @param applyId 应用主键
     * @author congcong tao
     * @date 2019/9/4 14:53
     */
    @BaseLog(module = "问卷调查", classz = "问卷调查", methods = "问卷调查列表", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/question/questionList")
    public ModelAndView questionList(String token, String applyId) {
        // 校验
        Map<String, Object> map = acceptToken(token, applyId, APPLY_TYPE_QUESTION);
        if (!(Boolean) map.get("flag")) {
            return (ModelAndView) map.get("modelAndView");
        }
        ModelAndView modelAndView = new ModelAndView("/mobile/question/questionList");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }

    /**
     * 获取问卷列表
     * @author congcong tao
     * @date 2018/12/20 8:42
     */
    @BaseLog(module = "问卷调查", classz = "问卷调查", methods = "问卷调查列表", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/question/jsonList")
    public ReturnResult questionJsonList(QuestionVo questionVo, String token, int pageSize, int pageNum) {
        // 分页
        WxMember wxMember = acceptToken(token);
        questionVo.setUserId(wxMember.getId());
        ReturnResult returnResult = questionService.pageLeft(questionVo, pageSize, pageNum);
        return returnResult;
    }

    /**
     * 学生问卷题目-任课老师问卷
     * @param token  用户信息
     * @param applyId  模块ID
     * @param tableId 问卷主键
     * @author congcong tao
     * @date 2018/12/20 11:36
     */
    @BaseLog(module = "问卷调查", classz = "问卷调查", methods = "学生问卷调查-任课老师问卷", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/question/questionEdit")
    public ModelAndView questionEdit(String token, String applyId, String questionId) {
        // 优化点，如果是扫码问卷，需要注意的是，当问卷已结束后，不能进入，或者进入到其他页面
        ModelAndView modelAndView = new ModelAndView("/mobile/question/questionEdit");
        WxMember wxMember = acceptToken(token);
        Question question = questionService.getInfo(questionId, wxMember.getId());
        modelAndView.addObject("questionnaire", question);
        modelAndView.addObject("createTime", DateUtil.formatDateTime(question.getCreateTime()));
        modelAndView.addObject("currentTime", DateUtil.formatDateTime(DateUtil.date()));
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }

    /**
     * 学生保存问卷
     * @param token  用户信息
     * @param studentScoreJson 学生保存问卷JSON
     * @author congcong tao
     * @date 2018/12/20 16:17
     */
    @BaseLog(module = "问卷调查", classz = "问卷调查", methods = "学生问卷调查保存", businessType = BusinessType.INSERT, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/questionnaire/saveStudentScore",method = RequestMethod.POST)
    public ReturnResult addOrUpdate(String token, String studentScoreJson){
        WxMember wxMember = acceptToken(token);
        ReturnResult returnResult = questionScoreService.saveQuestionScore(wxMember.getId(), studentScoreJson);
        return returnResult;
    }
}
